package com.kevin.es.repository;

import com.kevin.es.user.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.annotations.Highlight;
import org.springframework.data.elasticsearch.annotations.HighlightField;
import org.springframework.data.elasticsearch.annotations.Query;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author Kevin Liu
 * @date 2020/8/4 下午4:10
 */
@Repository
public interface UserRepository extends ElasticsearchRepository<User, String> {

    /**
     * 根据描述查询所有
     * @param desc desc
     * @return return the list of value
     */
    List<User> findByDesc(String desc);

    /**
     * 按照姓名查找
     * @param name name
     * @param pageable fenye
     * @return page
     */
    @Query("{\"match\": {\"name\": {\"query\": \"?0\"}}}")
    Page<User> testNativeQuery(String name, Pageable pageable);


    @Highlight(fields = {
            @HighlightField(name = "name"),
            @HighlightField(name = "desc")
    })
    List<SearchHit<User>> findByNameOrDesc(String name, String desc);
}
